package com.whfc.wxmp.redis;

import com.whfc.entity.dto.msg.PushMsgDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sun_guodong
 * @description
 * @date 2019-12-10
 */
@Service
public class StashWxmpMsgRedisDaoImpl implements StashWxmpMsgRedisDao {
    @Autowired
    private RedisTemplate<String, PushMsgDTO> redisTemplate;
    private static String KEY = "wxmp-push-msg-stash-queue::%s";

    @Override
    public void setStashMsg(Integer userId, PushMsgDTO pushMsgDTO) {
        ListOperations opsForList = redisTemplate.opsForList();
        opsForList.leftPush(String.format(KEY, userId), pushMsgDTO);
    }

    @Override
    public List<PushMsgDTO> getStashMsg(Integer userId) {
        List<PushMsgDTO> list = new ArrayList<>();
        Long size = redisTemplate.opsForList().size(String.format(KEY, userId));
        for (int i = 0; i < size; i++) {
            PushMsgDTO pushMsgDTO = redisTemplate.opsForList().leftPop(String.format(KEY, userId));
            list.add(pushMsgDTO);
        }
        return list;
    }
}
